# coding=utf-8
from learning.scientifc import writer
from learning.scientifc.jobs.job import Job
from learning.scientifc.readers.reader import ReaderFactory


class DuplicateLineJob(Job):

    def __init__(self, app):
        self.filename = app.config.get('DUPLICATE_LINE_FILENAME')
        self.factory = ReaderFactory(app)

    def run(self, *args, **kwargs):
        origin = self.factory.create(self.filename)
        data = []
        header = origin.row_values(0)
        year_index = 0
        for i, e in enumerate(header):
            if 'year' in e:
                # 找到 year 所在的列
                year_index = i
                break

        for i, row in origin.range(1):
            try:
                year = int(row[year_index])
                for y in range(year, 2018):
                    element = row[:year_index] + [y] + row[year_index + 1:]
                    replace = []
                    for e in element:
                        replace.append(str(e))
                    data.append(tuple(replace))
            except Exception as e:
                print('something wrong', e)

        writer.write_unlimited(data)
